<style>
#galleria{ height: 450px; }

.pure_form_extra { box-shadow: 5px 5px 5px #888888; padding: 10px; border: 1px solid #aaa; border-radius: 3px; }
.pure_form_extra * { color: #ddd; text-shadow: 2px 2px #333; font-family: '黑体', Trebuchet MS; }

.pure_form_extra2 { box-shadow: 5px 5px 5px #888888; padding: 10px; border: 1px solid #aaa; border-radius: 3px; }
.pure_form_extra2 .cs_catname a { color: #fff; text-shadow: 2px 2px #000; font-family: '黑体', Trebuchet MS; }
.pure_form_extra2 a { text-shadow: 2px 2px #fff; font-family: '黑体', Trebuchet MS; }
.il { display: inline-block !important; margin-bottom: 10px !important; }
.pure_table_extra { width: 448px; border: 1px solid #aaa !important; }
.pure_table_extra td { height: 20px !important; }
.pure_table_extra a { color: #666; }
.pure_table_extra * { font-size: 12px !important; font-family: Trebuchet MS !important; }
.pure_table_extra .date { float: right; color: #666; font-size: 11px !important; }

</style>
<?php 
$_SESSION[TINYMCE_UPLOAD] = 'upload/plugins/' . basename(dirname(__FILE__)) . '/';
if (!file_exists('../' . $_SESSION[TINYMCE_UPLOAD])) {
  Utils::create('../' . $_SESSION[TINYMCE_UPLOAD]);
}

$data = $orm->get('pb_ht_hm_data', NULL, NULL, 1, 1, NULL)->d;
if (count($data) == 0) {
  $obj = new stdClass();
  $obj->ad_top = '';
  $obj->ad_middle = '';
  $obj->ad_bottom = '';
  $obj->cats1 = '';
  $obj->cats2 = '';
  $orm->add('pb_ht_hm_data', $obj);
  $obj->id = $orm->last_id();
  $data[] = $obj;
}

$images = $orm->get('pb_ht_hm_image', NULL, NULL, NULL, NULL, NULL)->d;

$opts = new stdClass();
$opts->cd = 't.parent_id = :v1';
$opts->cdv = array(':v1' => 0);
$categories = $orm->get('pb_ht_category', NULL, NULL, NULL, NULL, $opts)->d;

if ($data[0]->cats1 != '') {
  $data[0]->cats1 = json_decode($data[0]->cats1);
} else {
  $data[0]->cats1 = array();
}

if ($data[0]->cats2 != '') {
  $data[0]->cats2 = json_decode($data[0]->cats2);
} else {
  $data[0]->cats2 = array();
}

$data[0]->__cats1 = array();
$data[0]->__cats2 = array();


foreach ($data[0]->cats1 as $itm) {
  if (($itm = __get_category($itm, $categories)) !== NULL) {
    $opts = new stdClass();
    $opts->cd = 't.categories__ LIKE :v1';
    $opts->cdv = array(':v1' => '%,' . $itm->id . ',%');
    $itm->__posts = $orm->get('pb_ht_article', 't.date', 'DESC', 1, 4, $opts)->d;
    $data[0]->__cats1[] = $itm;
  }
}

foreach ($data[0]->cats2 as $itm) {
  if (($itm = __get_category($itm, $categories)) !== NULL) {
    $opts = new stdClass();
    $opts->cd = 't.categories__ LIKE :v1';
    $opts->cdv = array(':v1' => '%,' . $itm->id . ',%');
    $itm->__posts = $orm->get('pb_ht_article', 't.date', 'DESC', 1, 5, $opts)->d;
    $data[0]->__cats2[] = $itm;
  }
}

function __get_category($id, $arr) {
  foreach ($arr as $itm) {
    if ($id == $itm->id) {
      return $itm;
    }
  }
  return NULL;
}

?>

<div id="galleria">
  <?php foreach ($images as $itm) { ?>
  <a href="<?php echo $itm->link; ?>">
    <img  src="<?php echo $itm->link; ?>" data-big="<?php echo $itm->link; ?>" data-title="" data-description=""></img>
  </a>
  <?php } ?>
</div>
<div>&nbsp;</div>
<div class="pure-form pure_form_extra cs_trans">
  <?php echo $data[0]->ad_top; ?>
</div>
<div>&nbsp;</div>

<?php foreach ($data[0]->__cats1 as $itm) { ?>
<div class="pure-form pure_form_extra2 il cs_cat">
  <div style="padding: 5px 10px; font-size: 13px; font-weight: bold;" class="cs_catname"><a href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=<?php echo ART_ID; ?>&cat=<?php echo $itm->id; ?>');""><?php echo $itm->name; ?></a></div>
  <table class="pure-table pure_table_extra" cellPadding="0" cellSpacing="0">
    <?php foreach ($itm->__posts as $idx => &$itm2) { ?>
    <tr class="<?php echo ($idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd'; ?>">
      <td>
        <span class="date"><?php echo date('Y-m-d', strtotime($itm2->date)); ?></span>
        <a href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=<?php echo ART_ID; ?>&__file=index_article&id=<?php echo $itm2->id?>');"><?php echo Utils::get_short($itm2->column__ . ' ' . $itm2->title, 30);?></a>
      </td>
    </tr>
    <?php } ?>
    <?php for ($i = 5 - count($itm->__posts), $il = 0; $i > $il; $i--) { ?>
    <tr class="<?php echo ($i % 2 == 0) ? 'pure-table-odd' : 'pure-table-even'; ?>"><td>&nbsp;</td></tr>
    <?php } ?>
  </table>
</div>
<?php } ?>

<div>&nbsp;</div>
<div class="pure-form pure_form_extra cs_trans">
  <?php echo $data[0]->ad_middle; ?>
</div>
<div>&nbsp;</div>

<?php foreach ($data[0]->__cats2 as $itm) { ?>
<div class="pure-form pure_form_extra2 il cs_cat">
  <div style="padding: 5px 10px; font-size: 13px; font-weight: bold;" class="cs_catname"><a href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=<?php echo ART_ID; ?>&cat=<?php echo $itm->id; ?>');""><?php echo $itm->name; ?></a></div>
  <table class="pure-table pure_table_extra" cellPadding="0" cellSpacing="0">
    <?php foreach ($itm->__posts as $idx => &$itm2) { ?>
    <tr class="<?php echo ($idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd'; ?>">
      <td>
        <span class="date"><?php echo date('Y-m-d', strtotime($itm2->date)); ?></span>
        <a href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=<?php echo ART_ID; ?>&__file=index_article&id=<?php echo $itm2->id; ?>');"><?php echo Utils::get_short($itm2->column__ . ' ' . $itm2->title, 30);?></a>
      </td>
    </tr>
    <?php } ?>
    <?php for ($i = 5 - count($itm->__posts), $il = 0; $i > $il; $i--) { ?>
    <tr class="<?php echo ($i % 2 == 0) ? 'pure-table-odd' : 'pure-table-even'; ?>"><td>&nbsp;</td></tr>
    <?php } ?>
  </table>
</div>
<?php } ?>

<div>&nbsp;</div>
<div class="pure-form pure_form_extra cs_trans">
  <?php echo $data[0]->ad_bottom; ?>
</div>
<div>&nbsp;</div>
        
<script type="text/javascript">
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  /*<![CDATA[*/
  $(function(){
    Galleria.run('#galleria');
  });

  /*]]>*/
</script>